package dao

import (
	"context"
	"sale-admin/internal/models"

	"gorm.io/gorm"
)

type ExpressFeeSetting struct{}

type ExpressFeeSettingT struct {
	ID                   string           `json:"id"`
	Name                 string           `json:"name"`
	ShippingFromProvince string           `json:"shipping_from_province"`
	ShippingFromCity     string           `json:"shipping_from_city"`
	PricingType          int              `json:"pricing_type"`
	FreeShippingProvince string           `json:"free_shipping_province"`
	Default              int              `json:"default"`
	PricingConfig        []PricingConfigT `json:"pricing_config"`
	GoodsCount           int64            `json:"goods_count"` // 使用该模版的商品统计
}

type PricingConfigT struct {
	Province string `json:"province"`
	Config   []struct {
		Number int     `json:"number"`
		Price  float64 `json:"price"`
	} `json:"config"`
}

// Create 新增
func (ExpressFeeSetting) Create(
	ctx context.Context,
	db *gorm.DB,
	record models.ExpressFeeSetting,
) (id string, err error) {
	var count int64
	db.WithContext(ctx).Model(&models.ExpressFeeSetting{}).
		Where("`default` = ?", 1).
		Count(&count)
	if count == 0 {
		record.Default = 1
	}
	err = db.WithContext(ctx).Model(&models.ExpressFeeSetting{}).Create(&record).Error
	if err != nil {
		return "", err
	}
	return record.ID, nil
}

// Update 更新
func (ExpressFeeSetting) Update(
	ctx context.Context,
	db *gorm.DB,
	ID string,
	record models.ExpressFeeSetting,
) bool {
	var recordMap = map[string]interface{}{}
	if record.Name != "" {
		recordMap["name"] = record.Name
	}
	recordMap["shipping_from_province"] = record.ShippingFromProvince
	recordMap["shipping_from_city"] = record.ShippingFromCity
	if record.PricingType > 0 {
		recordMap["pricing_type"] = record.PricingType
	}
	recordMap["free_shipping_province"] = record.FreeShippingProvince
	recordMap["pricing_config"] = record.PricingConfig
	err := db.WithContext(ctx).Model(&models.ExpressFeeSetting{}).
		Where("id = ?", ID).
		Updates(&recordMap).Error
	return err == nil
}

// List 列表
func (ExpressFeeSetting) List(
	ctx context.Context,
	db *gorm.DB,
	name string,
	pricingType int, // 计价方式 1-计件 2-计重
	page, limit int,
) (list []models.ExpressFeeSetting, total int64) {
	tx := db.WithContext(ctx).Model(&models.ExpressFeeSetting{})
	if name != "" {
		tx = tx.Where("name LIKE ?", "%"+name+"%")
	}
	if pricingType > 0 {
		tx = tx.Where("pricing_type = ?", pricingType)
	}
	tx.Count(&total)
	if limit > 0 {
		if limit > 2000 {
			limit = 2000
		}
		tx = tx.Limit(limit)
	}
	if page > 0 && limit > 0 {
		tx = tx.Offset((page - 1) * limit)
	}
	tx.Find(&list)
	return
}

// Detail 详情
func (ExpressFeeSetting) Detail(
	ctx context.Context,
	db *gorm.DB,
	ID string,
) *models.ExpressFeeSetting {
	var record models.ExpressFeeSetting
	rowsAffected := db.WithContext(ctx).Model(&models.ExpressFeeSetting{}).
		Where("id = ?", ID).
		Take(&record).
		RowsAffected
	if rowsAffected <= 0 {
		return nil
	}
	return &record
}

// Delete 删除
func (ExpressFeeSetting) Delete(
	ctx context.Context,
	db *gorm.DB,
	ID string,
) error {
	return db.WithContext(ctx).Model(&models.ExpressFeeSetting{}).
		Where("id = ?", ID).
		Delete(&models.ExpressFeeSetting{}).
		Error
}

func (ExpressFeeSetting) GetDefault(
	ctx context.Context,
	db *gorm.DB,
) *models.ExpressFeeSetting {
	var record models.ExpressFeeSetting
	rowsAffected := db.WithContext(ctx).Model(&models.ExpressFeeSetting{}).
		Where("`default` = ?", 1).
		Take(&record).RowsAffected
	if rowsAffected <= 0 {
		return nil
	}
	return &record
}
